<?php

class edit_eventsignups
{
	var $data;
	var $eventid;
	
	var $add_record;
	var $valid;

	function edit_eventsignups($add_record, $eventid, $data)
	{
		global $db, $lang;
        
		$this->add_record = $add_record;
		$this->eventid = $eventid;
		$this->data = $data;
	}
		
	function render()
	{		
		global $skin_dir, $lang;
		
		if($this->add_record)
		{
			$eventid = $this->eventid ;
			
			$driverOptions = '';
			
			foreach($this->data as $driver)
			{
				
				$driverid = $driver['user_id'];
				$carNo = $driver['cs_no'] == 0 ? '' : $driver['cs_no'] . ' ';
				$drivername = $driver['user_drivername'];
				$car = $driver['c_id'] == '' ? $lang['Unassigned'] : $driver['c_manufacturer'] . ' ' . $driver['c_model'];
				$team = stripslashes($driver['t_id'] == '' ? $lang['Independent'] : $driver['t_name']);
				$division = $driver['d_title'];
					
				$driverOptions .= '<option value="' . $driverid . '">' . $carNo  . $drivername . ' (' .
									$team . ') (' . $car  . ') (' . $division   . ')</option>';
			}
			 
    		include $skin_dir . 'admin/addeventsignup.tpl';
		}
		else
		{
			$eventid = $this->eventid ;
			$driverid = $this->data['user_id'];
			$carNo = $this->data['es_no'] == 0 ? '' : $this->data['es_no'];
			$driver = $this->data['user_drivername'];
			$car = $this->data['c_id'] == '' ? $lang['Unassigned'] : $this->data['c_manufacturer'] . ' ' . $this->data['c_model'];
			$team = stripslashes($this->data['t_id'] == '' ? $lang['Independent'] : $this->data['t_name']);
			$division = $this->data['d_title'];
			$signuptime = $this->data['es_signuptime'];
			 
    		include $skin_dir . 'admin/editeventsignup.tpl';
			
		}
	}
}

class eventsignups
{
	var $mess;
	var $eventid;
    
    var $champEntrants = Array();
	var $eventEntrants = Array();
    
	var $toRender = Array();

	function eventsignups($eventid)
	{
		$mess = '';
		$this->eventid = $eventid;
		if(isset($_POST['removeRecord']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']))
		{
			$this->doAdd();
		}
		$this->setupEdit();
		$this->setupAdd();
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		foreach( $this->toRender as $key => $value){
			$value->render();
		}
		
	}
	
	function doDelete()
	{
		global $db, $lang;
		
		$qUpdateChamps = 'SELECT pf_drivername as user_drivername FROM ' . DRIVERNAME_TABLE . ' WHERE user_id = ' . $_POST['driver'];
		$result = $db->sql_query($qUpdateChamps);
		if($row = $db->sql_fetchrow($result))
		{
			//get the record
			$qDel = "delete from ". EVENTSIGNUPS_TABLE . " Where es_event = " . $this->eventid . " AND es_driver = " . $_POST['driver'];
			$db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['user_drivername'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
				
		//confirm driver is signed up for champ and get their details	
		$qSel = "select *, pf_drivername as user_drivername 
				 from " . USERS_TABLE . " u, " . CHAMPSIGNUPS_TABLE . ", " . CHAMPIONSHIPS_TABLE . ", " . 
				 		  EVENTS_TABLE .  ", " . DRIVERNAME_TABLE .  " d " .
				" WHERE u.user_id = cs_driver AND cs_champ = c_id AND u.user_id = d.user_id AND e_championship = c_id AND e_id = " . 
						$this->eventid . " AND u.user_id = " . $_POST['driver'];

		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result))
		{
			$qUpd = "insert into  ". EVENTSIGNUPS_TABLE . " (es_no, es_driver, es_car, es_team, es_event, es_division, es_signuptime, es_entryOwner) values(" .
					 $row['cs_no'] . ", " . $row['user_id'] . ", " .  $row['cs_car'] . ", "  . $row['cs_team'] . ", " . 
					  $row['e_id'] . ", ". $row['cs_div'] .  ", NOW(), " . $row['cs_entryOwner'] . " )";
			$db->sql_query($qUpd);
			$this->mess = new addMessage(str_replace('{entry}', $row['user_drivername'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
			
	}
	
	function setupEdit()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('includes/renderInclude.php');
		
		              
        //get the record
		
		$q = "SELECT *, pf_drivername as user_drivername," .
					'(SELECT t.t_id FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = es_team LIMIT 0, 1) AS t_id,'.
					'(SELECT t.t_name FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = es_team LIMIT 0, 1) AS t_name'.
					" FROM " . EVENTSIGNUPS_TABLE . ", " . USERS_TABLE . " u, " . DRIVERNAME_TABLE . " d, " . CARS_TABLE  . ", " .  DIVISIONS_TABLE  . 
					" WHERE u.user_id = d.user_id AND u.user_id = es_driver AND c_id = es_car AND 
							d_id = es_division AND es_event = " . $this->eventid . 
					" ORDER BY es_no, pf_drivername ASC";
		$result = $db->sql_query($q);
		
		$introMess = false;
		
		while($row = $db->sql_fetchrow($result)) 
		{
			if(!$introMess)
			{
				$this->toRender[] = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
				$this->toRender[] = new renderInclude('tpl/tableStart.tpl');
				$introMess = true;
			}
			
			$this->toRender[] = new edit_eventsignups(false, $this->eventid, $row);
		}
		
		if($introMess)
		{
			$this->toRender[] = new renderInclude('tpl/tableEnd.tpl');
		}
	}

	function setupAdd()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('includes/renderInclude.php');
				
		$q = "SELECT *, pf_drivername as user_drivername," .
					'(SELECT t.t_id FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = cs_team LIMIT 0, 1) AS t_id,'.
					'(SELECT t.t_name FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = cs_team LIMIT 0, 1) AS t_name'.
						" FROM " . CHAMPSIGNUPS_TABLE . ", " . USERS_TABLE ." u, " .  DIVISIONS_TABLE  .	", " .  
								   EVENTS_TABLE  . ", " .  DRIVERNAME_TABLE  . " d, " .  CARS_TABLE  . 
					" WHERE u.user_id = d.user_id AND u.user_id = cs_driver AND d_id = cs_div AND cs_champ = e_championship AND cs_car = c_id AND " .
					" e_id = " . $this->eventid .  " AND cs_driver NOT IN (SELECT es_driver FROM " . EVENTSIGNUPS_TABLE . " WHERE es_event = " . 
						$this->eventid . ") ORDER BY cs_no, pf_drivername ASC";

		$result = $db->sql_query($q);
		
		$introMess = false;
		$drivers = Array();
		while($row = $db->sql_fetchrow($result)) 
		{
			$drivers[] = $row;
		}
		
		$this->toRender[] = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$this->toRender[] = new renderInclude('tpl/tableStart.tpl');
		$this->toRender[] = new edit_eventsignups(true, $this->eventid, $drivers);
		$this->toRender[] = new renderInclude('tpl/tableEnd.tpl');
		
		
	}

	
}
?>